package com.highspeed.bdk.gateway.socket.analyze.message.gui.resolver;

import com.highspeed.bdk.gateway.Util;
import com.highspeed.bdk.gateway.socket.analyze.message.gui.base.MessageResolver;

import java.util.*;

/**
 * 3.11物理端口消息查询的反馈包
 * typedef struct _physicalPort
 * {
 * U32 physicalPortId; //16-bit rionet id + 16-bit portId
 * U32 physicalAddr;//32-bit 物理地址
 * <p>
 * U8   reserved: 1,
 * portQuality: 3, //端口质量等级
 * portMode: 4;   //端口模式,0001 1x,0010 4x
 * <p>
 * U8   reserved1;
 * U16 routeItems;//路录表项的个数
 * <p>
 * U32 bandwidthCap; //端口能力带宽（Mbps）
 * U32 bandwidthAvail; //端口实际平均带宽
 * struct _portRouteItem routeTable[0]; //端口路录表，单播,初始化上报时不提供
 * <p>
 * } PhysicalPortFbData;
 */
public class PhysicalPortResolver0x05 extends MessageResolver {

    @Override
    protected Map doResolveBody(byte[] response) {

        Map result = new HashMap();
        short rionetId = Util.getShort(response, getOffset());
        result.put("rionetId", rionetId);
        offset2();
        short portId = Util.getShort(response, getOffset());
        offset2();
        logger.info("rionetId........" + rionetId + ",portId......." + portId);
        result.put("portId", portId);

        int physicalAddr = Util.getInt(response, getOffset());
        offset4();
        logger.info("physicalAddr........." + physicalAddr);
        result.put("physicalAddr", physicalAddr);

        byte u8 = Util.getByte(response, getOffset());
        offset1();
        logger.info("u8..............." + u8);
        result.put("u8", u8);

        byte reserved1 = Util.getByte(response, getOffset());
        offset1();
        logger.info("reserved1.........." + reserved1);

        short routeItems = Util.getShort(response, getOffset());//路录表项的个数
        offset2();
        logger.info("routeItems.........." + routeItems);
        result.put("routeItems", routeItems);

        int bandwidthCap = Util.getInt(response, getOffset()); //端口能力带宽（Mbps）
        offset4();
        logger.info("bandwidthCap.........." + bandwidthCap);
        result.put("bandwidthCap", bandwidthCap);

        int bandwidthAvail = Util.getInt(response, getOffset()); //端口实际平均带宽
        offset4();
        logger.info("bandwidthAvail.........." + bandwidthAvail);
        result.put("bandwidthAvail", bandwidthAvail);

        List<Map> routes = null;
        if (routeItems > 0) {
            routes = new ArrayList<>();
            result.put("routes", routes);
        }

        for (int nodeSize = 0; nodeSize < routeItems; nodeSize++) {
            Map routeMap = new HashMap();
            routes.add(routeMap);
            Short m_inPortId = Util.getShort(response, getOffset());
            offset2();
            logger.info("m_inPortId..........." + m_inPortId);
            routeMap.put("m_inPortId", m_inPortId);

            Short m_dstID = Util.getShort(response, getOffset());
            offset2();
            logger.info("m_dstID..........." + m_dstID);
            routeMap.put("m_dstID", m_dstID);

            Short outPorts = Util.getShort(response, getOffset());
            offset2();
            routeMap.put("outPorts", outPorts);

            Short nextHops = Util.getShort(response, getOffset());
            offset2();
            routeMap.put("nextHops", nextHops);

            short[] m_outPortIds = new short[outPorts];
            short[] m_nextHops = new short[nextHops];

            List m_outPortIds1 = new ArrayList();
            routeMap.put("m_outPortIds", m_outPortIds1);

            for (short i = 0; i < outPorts; i++) {
                Short outport = Util.getShort(response, getOffset());
                offset2();
                m_outPortIds1.add(outport);
                m_outPortIds[i] = outport;
            }

            List m_nextHops1 = new ArrayList();
            routeMap.put("m_nextHops", m_nextHops1);
            for (short i = 0; i < nextHops; i++) {
                Short nextHop = Util.getShort(response, getOffset());
                m_nextHops1.add(nextHop);
                offset2();
                m_nextHops[i] = nextHop;
            }
            logger.info("outPorts=" + outPorts + ",m_outPortIds=" + Arrays.toString(m_outPortIds) + "......nextHops=" + nextHops + ",m_nextHops=" + Arrays.toString(m_nextHops));
        }
        getContainer().put("phyPort", result);
        return getContainer();
    }
}
